﻿// License: The MIT License (MIT) Copyright (c) 2011..2012 Barend Gehrels

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using Stormy;

namespace GrooveSharp
{
    // Contains all logic from/to the database for the UI (FullScreenInfo)
    public class FlagManager
    {
        private int m_currentTrackId = 0;
        private bool m_FlagChanged = false;
        private IList<Flag> m_currentFlags;

        public FlagManager(int id)
        {
            m_currentTrackId = id;
            if (m_currentTrackId > 0)
            {
                m_currentFlags = Db.Connection().Select<Flag>
                    (
                        @"
                            select f.*
                                ,(select k.is_active
                                    from link_track_flag k
                                    where k.track_id=@track and k.track_flag_id=f.track_flag_id
                                ) as flagged_for_track
                            from track_flags f
                            where f.is_active = 1
                        ",
                        new List<StormyParameter>() 
                            { 
                                Db.CreateParameter(DbType.Int32, "@track", m_currentTrackId) 
                            }

                    ).ToList<Flag>();

            }
        }

        public int TrackId { get { return m_currentTrackId; } }

        public bool flagChanged
        {
            set
            {
                m_FlagChanged = value;
            }
        }

        public IList<Flag> flags
        {
            get
            {
                return m_currentFlags;
            }
        }

        public void save()
        {
            if (m_currentTrackId > 0 && m_FlagChanged)
            {
                App.Log.InfoFormat("Saving flags for {0}", m_currentTrackId);

                // Delete old flags
                Db.Connection().Execute(String.Format("delete from link_track_flag where track_id={0}", m_currentTrackId));

                // Insert new flags (active only) (there is not yet a mapper for it)
                foreach (Flag flag in (from item in m_currentFlags where item.Flagged select item))
                {
                    Db.Connection().Execute(String.Format("insert into link_track_flag(track_id,track_flag_id,user_id,is_active) values({0},{1},{2},{3})",
                        m_currentTrackId, flag.Id, Db.UserId(), 1));
                }
            }
            m_currentTrackId = 0;
            m_FlagChanged = false;
        }

    }
}
